package com.ty.servlet;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class SessionListener implements HttpSessionListener {
    private static int activeSessions = 0;

    public SessionListener() {
        System.out.println("SessionListener初始化");
    }
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        synchronized (this) {
            activeSessions++;
        }
        System.out.println("新会话创建，当前活动会话数: " + activeSessions);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        synchronized (this) {
            activeSessions--;
        }
        System.out.println("会话销毁，当前活动会话数: " + activeSessions);
    }
}